package com.ruoyi.quality.service.impl;

import java.util.*;
import java.util.stream.Collectors;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.ruoyi.common.annotation.Excel;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.quality.domain.TIndexData2;
import com.ruoyi.quality.domain.dto.*;
import com.ruoyi.safe.domain.*;
import com.ruoyi.safe.domain.dto.BizRectifyReviewDTO;
import com.ruoyi.safe.mapper.BizRectifyReviewMapper;
import com.ruoyi.safe.mapper.CommonProjectMapper;
import com.ruoyi.safe.service.*;
import com.ruoyi.system.mapper.SysDeptMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.ruoyi.quality.mapper.TQualityFileItemMapper;
import com.ruoyi.quality.domain.TQualityFileItem;
import com.ruoyi.quality.service.ITQualityFileItemService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 质量问题拆分明细Service业务层处理
 * 
 * @author mahl2
 * @date 2024-02-05
 */
@Service
public class TQualityFileItemServiceImpl implements ITQualityFileItemService 
{
    @Value("${minio.bucketName}")
    private String bucketName;
    @Autowired
    private TQualityFileItemMapper tQualityFileItemMapper;
    @Autowired
    private IBizRectifyReviewService iBizRectifyReviewService;
    @Autowired
    private IBizExtensionRequestsService bizExtensionRequestsService;
    @Autowired
    private IBizSafeFineService bizSafeFineService;
    @Autowired
    private IBizSafeBillnoService bizSafeBillnoService;

    @Autowired
    private IBizSafeAttachService bizSafeAttachService;
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private SysDeptMapper sysDeptMapper;
    @Autowired
    private CommonProjectMapper commonProjectMapper;

    @Autowired
    IBizSafeAttachService fileAttachService;

    /**
     * 查询质量问题拆分明细
     * 
     * @param id 质量问题拆分明细主键
     * @return 质量问题拆分明细
     */
    @Override
    public TQualityFileItem selectTQualityFileItemById(Long id)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        TQualityFileItem t = new TQualityFileItem();
        t.setId(id);t.setCheckPeopleId(String.valueOf(loginUser.getUserId()));
        TQualityFileItem tQualityFileItem = tQualityFileItemMapper.selectTQualityFileItemById(t);
        if(StringUtils.isNotNull(tQualityFileItem.getFileIds()) && !"".equals(tQualityFileItem.getFileIds())){
            String[] arr = tQualityFileItem.getFileIds().split(",");
            Long[] photoIds = new Long[arr.length];
            for(int i=0;i<arr.length;i++){
                photoIds[i] = Long.parseLong(arr[i]);
            }
            List<BizSafeAttach> bizSafeAttaches = bizSafeAttachService.selectBizSafeAttachByBizIds(photoIds);
            tQualityFileItem.setFileIdsArr(bizSafeAttaches);


        }
        /* 字典转意*/
        tQualityFileItem.setDataSourceName(DictUtils.getDictLabel("list_quality_data_source", tQualityFileItem.getDataSource()));
        tQualityFileItem.setDataStatusName(DictUtils.getDictLabel("list_quality_data_status", tQualityFileItem.getDataStatus()));
        tQualityFileItem.setRectificationDeadlineStatusName(DictUtils.getDictLabel("list_quality_rectification_deadline_status", tQualityFileItem.getRectificationDeadlineStatus()));
        tQualityFileItem.setQuestionBeyondName(DictUtils.getDictLabel("list_quality_question_beyond", tQualityFileItem.getQuestionBeyond()));
        tQualityFileItem.setQuestionAnalysisName(DictUtils.getDictLabel("list_quality_question_analysis", tQualityFileItem.getQuestionAnalysis()));
        tQualityFileItem.setQuestionLevelName(DictUtils.getDictLabel("list_quality_level", tQualityFileItem.getQuestionLevel()));
        tQualityFileItem.setReworkOrNotName(DictUtils.getDictLabel("list_quality_rework_or_not", tQualityFileItem.getReworkOrNot()));
        tQualityFileItem.setBucket(bucketName);
        return tQualityFileItem;
    }

    /**
     * 查询质量问题拆分明细列表
     * 
     * @param tQualityFileItem 质量问题拆分明细
     * @return 质量问题拆分明细
     */
    @Override
    public List<TQualityFileItem> selectTQualityFileItemList(TQualityFileItem tQualityFileItem)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        tQualityFileItem.setCheckPeopleId(String.valueOf(loginUser.getUserId()));
        List<TQualityFileItem> list = tQualityFileItemMapper.selectTQualityFileItemList(tQualityFileItem);
        for (TQualityFileItem item : list)
        {
            if(item.getFileIds()!=null && !"".equals(item.getFileIds())){
                BizSafeAttach bizSafeAttach = new BizSafeAttach();
                String[] strlist = item.getFileIds().split(",");
                List<Long> listIds = new ArrayList<Long>(strlist.length);
                for (int i = 0; i < strlist.length; ++i)
                {
                    listIds.add( Long.parseLong(strlist[i]));
                }
                bizSafeAttach.setIds(listIds);
                /** 资料文档附件列表 */
                List<BizSafeAttach> fileDocAttachList = fileAttachService.selectBizSafeAttachListIds(bizSafeAttach);
                if(fileDocAttachList != null && fileDocAttachList.size()> 0){
                    for(int n = 0 ; n< fileDocAttachList.size() ;n++){
                        BizSafeAttach attach = fileDocAttachList.get(n);
                        if(!attach.getType().contains("image")){
                            fileDocAttachList.remove(attach);
                        }
                    }
                    item.setAttachList(fileDocAttachList);
                }else{
                    item.setAttachList(new ArrayList<>());
                }

            }else{
                item.setAttachList(new ArrayList<>());
            }

            item.setBucket(bucketName);
            //如果当前问题没有查到检查人，则此问题若有发文单位，则发文单位即为检查人名称
            if (item.getCheckPeopleName() == null || item.getCheckPeopleName().isEmpty())
            {
                if (item.getFileCompanyName() != null && !item.getFileCompanyName().isEmpty())
                {
                    item.setCheckPeopleName(item.getFileCompanyName());
                }
            }
            if (item.getReviseTheDates() == null)
            {
                item.setOverdue("0");
            }
            else
            {
                Date last = item.getReviseTheDates();
                last.setHours(23);
                last.setMinutes(59);
                last.setSeconds(59);
                int v = last.compareTo(DateUtils.getNowDate());
                item.setOverdue(v >= 0 ? "0" : "1");
            }
            if("3".equals(item.getUserType())){
                if("0".equals(item.getDataStatus())){
                    item.setUserType("1");
                }else if("1".equals(item.getDataStatus())){
                    item.setUserType("2");
                }else{
                    item.setUserType("4");
                }
            }
        }
        return list;
    }

    /**
     * 查询质量问题我的待办列表
     *
     * @param tQualityFileItem 质量问题我的待办列表
     * @return 质量问题我的待办列表
     */
    @Override
    public List<TQualityFileItem> selectCheckLedger(TQualityFileItem tQualityFileItem)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        tQualityFileItem.setLoginUserId(loginUser.getUserId().toString());
        List<TQualityFileItem> list = tQualityFileItemMapper.selectCheckLedger(tQualityFileItem);
        for (TQualityFileItem item : list)
        {
            item.setBucket(bucketName);
            // 超期计算
            if (item.getReviseTheDates() == null)
            {
                item.setOverdue("0");
            }
            else
            {
                Date last = item.getReviseTheDates();
                last.setHours(23);
                last.setMinutes(59);
                last.setSeconds(59);
                int v = last.compareTo(DateUtils.getNowDate());
                item.setOverdue(v >= 0 ? "0" : "1");
            }

            //如果当前问题没有查到检查人，则此问题若有发文单位，则发文单位即为检查人名称
            if (item.getCheckPeopleName() == null || item.getCheckPeopleName().isEmpty())
            {
                if (item.getFileCompanyName() != null && !item.getFileCompanyName().isEmpty())
                {
                    item.setCheckPeopleName(item.getFileCompanyName());
                }
            }

            if(item.getFileIds()!=null && !"".equals(item.getFileIds())){
                BizSafeAttach bizSafeAttach = new BizSafeAttach();
                String[] strlist = item.getFileIds().split(",");
                List<Long> listIds = new ArrayList<Long>(strlist.length);
                for (int i = 0; i < strlist.length; ++i)
                {
                    listIds.add( Long.parseLong(strlist[i]));
                }
                bizSafeAttach.setIds(listIds);
                /** 资料文档附件列表 */
                List<BizSafeAttach> fileDocAttachList = fileAttachService.selectBizSafeAttachListIds(bizSafeAttach);
                for(int x = 0 ; x<fileDocAttachList.size(); x++){
                    BizSafeAttach attach = fileDocAttachList.get(x);
                    if(!attach.getType().contains("image")){
                        fileDocAttachList.remove(attach);
                    }
                }
                item.setAttachList(fileDocAttachList);
            }else{
                item.setAttachList(new ArrayList<>());
            }

        }
        return list;
    }

    /**
     * 查询质量问题操作明细
     *
     * @param id 质量问题拆分主键
     * @return 质量问题操作明细
     */
    public TQualityFileItemReviewDTO selectReviewDetail(Long id)
    {
        TQualityFileItemReviewDTO tQualityFileItemReview = new TQualityFileItemReviewDTO();
        tQualityFileItemReview.setBucket(bucketName);
        /** 整改数据 */
        TQualityFileItem tQualityFileItem = selectTQualityFileItemById(id);

        if (tQualityFileItem == null)
        {
            tQualityFileItemReview.settQualityFileItem(null);
        }
        else
        {
            // 超期计算
            if (tQualityFileItem.getReviseTheDates() == null)
            {
                tQualityFileItem.setOverdue("0");
            }
            else
            {
                Date last = tQualityFileItem.getReviseTheDates();
                last.setHours(23);
                last.setMinutes(59);
                last.setSeconds(59);
                int v = last.compareTo(DateUtils.getNowDate());
                tQualityFileItem.setOverdue(v >= 0 ? "0" : "1");
            }

            //如果当前问题没有查到检查人，则此问题若有发文单位，则发文单位即为检查人名称
            if (tQualityFileItem.getCheckPeopleName() == null || tQualityFileItem.getCheckPeopleName().isEmpty())
            {
                if (tQualityFileItem.getFileCompanyName() != null && !tQualityFileItem.getFileCompanyName().isEmpty())
                {
                    tQualityFileItem.setCheckPeopleName(tQualityFileItem.getFileCompanyName());
                }
            }
            tQualityFileItem.setBucket(bucketName);
            tQualityFileItemReview.settQualityFileItem(tQualityFileItem);
        }

        BizRectifyReview bizSelect = new BizRectifyReview();
        bizSelect.setRecordId(id);
        /** 整改操作数据 */
        List<BizRectifyReview> listReview = iBizRectifyReviewService.selectBizRectifyReviewList2(bizSelect);

        List<TQualityReviewDTO> listTQualityReviews = new ArrayList<>();
        for (BizRectifyReview item : listReview)
        {
            TQualityReviewDTO tQualityReview = new TQualityReviewDTO();
            tQualityReview.setBucket(bucketName);
            tQualityReview.setBizRectifyReview(item);

            if (item.getOperatorType().equals("1"))
            {
                BizSafeFine bizSafeFine  = new BizSafeFine();
                bizSafeFine.setRecordId(id);
                bizSafeFine.setBizRectifyReviewId(item.getId());
                List<BizSafeFine> listBizSafeFine = bizSafeFineService.selectBizSafeFineList(bizSafeFine);
                if (listBizSafeFine == null)
                {
                    tQualityReview.setListbizSafeFine(new ArrayList<>());
                }
                else
                {
                    tQualityReview.setListbizSafeFine(listBizSafeFine);
                }
            }
            tQualityReview.setOperatorExplain(item.getOperatorExplain());
            tQualityReview.setQuestionOrigin(tQualityFileItemReview.gettQualityFileItem().getQuestionOrigin());
            tQualityReview.setUploadIds(item.getUploadIds());
            tQualityReview.setOperatorerPhonenumber(item.getOperatorerPhonenumber());
            tQualityReview.setRemoveUserPhonenumber(item.getRemoveUserPhonenumber());
            listTQualityReviews.add(tQualityReview);
        }
        if (listTQualityReviews == null)
        {
            tQualityFileItemReview.setListTQualityReview(new ArrayList<>());
        }
        else
        {
            tQualityFileItemReview.setListTQualityReview(listTQualityReviews);
        }

        /** 移交操作数据 */
        bizSelect.setOperatorType("3");
        List<BizRectifyReview> listTransfer = iBizRectifyReviewService.selectBizRectifyReviewList(bizSelect);
        if (listTransfer == null)
        {
            tQualityFileItemReview.setListTransfer(new ArrayList<>());
        }
        else
        {
            for (BizRectifyReview item : listTransfer)
            {
                item.setRemoveUserName(item.getRemoveName());
                item.setOperatorerName(item.getOperatorerName());
            }
            tQualityFileItemReview.setListTransfer(listTransfer);
        }

        /** 审批操作数据 */
        BizExtensionRequests bizExtensionRequests = new BizExtensionRequests();
        bizExtensionRequests.setRecordId(id);
        LoginUser loginUser = SecurityUtils.getLoginUser();
        bizExtensionRequests.setUserId(String.valueOf(loginUser.getUserId()));
        List<BizExtensionRequests> listCheck = bizExtensionRequestsService.selectBizExtensionRequestsList(bizExtensionRequests);
        if (listCheck == null)
        {
            tQualityFileItemReview.setListTQualityCheck(new ArrayList<>());
        }
        else
        {
            tQualityFileItemReview.setListTQualityCheck(listCheck);
        }
        tQualityFileItemReview.setExtensionId(tQualityFileItem.getExtensionId());
        return tQualityFileItemReview;
    }
    /**
     * 查询质量问题操作明细-前端要的操作详情
     *
     * @param id 质量问题拆分主键
     * @return 质量问题操作明细
     */
    public TQualityFileItemReviewDTO2 selectReviewDetail2(Long id)
    {
        TQualityFileItemReviewDTO tQualityFileItemReviewDTO = selectReviewDetail(id);
        TQualityFileItemReviewDTO2 tQualityFileItemReviewDTO2 = new TQualityFileItemReviewDTO2();
        tQualityFileItemReviewDTO2.setBucket(bucketName);
        if (tQualityFileItemReviewDTO == null)
        {
            tQualityFileItemReviewDTO2.setListReview(new ArrayList<>());
            tQualityFileItemReviewDTO2.setListCheck(new ArrayList<>());
            tQualityFileItemReviewDTO2.setListTransfer(new ArrayList<>());
            return tQualityFileItemReviewDTO2;
        }

        /** 整改操作数据 */
        List<TQualityListReviewDTO> listReview = new ArrayList<>();
        List<TQualityReviewDTO> list1 = tQualityFileItemReviewDTO.getListTQualityReview();
        for (TQualityReviewDTO item : list1)
        {
            TQualityListReviewDTO item2 = new TQualityListReviewDTO();
            item2.setBucket(bucketName);

            /**
             * 记录id
             */
            item2.setRecordId(item.getBizRectifyReview().getRecordId());
            /**
             * 序号
             */
            item2.setId(item.getBizRectifyReview().getId());

            /**
             * 操作状态名称
             */
            item2.setOperatorStatusName(item.getBizRectifyReview().getOperatorStatus());

            /**
             * 操作人
             */
            item2.setOperatorer(item.getBizRectifyReview().getOperatorerName());

            /**
             * 操作时间
             */
            item2.setOperatorTime(item.getBizRectifyReview().getOperatorTime());

            /**
             * 操作类型(1 复查，2 整改)
             */
            item2.setOperatorType(item.getBizRectifyReview().getOperatorType());
            /*成因*/
            item2.setQuestionOrigin(item.getQuestionOrigin()==null?"":item.getQuestionOrigin());
            /*复查说明*/
            item2.setOperatorExplain(item.getOperatorExplain()==null?"":item.getOperatorExplain());
            item2.setOperatorerPhonenumber(item.getOperatorerPhonenumber()==null?"":item.getOperatorerPhonenumber());
            BizSafeAttach bizSafeAttach = new BizSafeAttach();
            if(item.getUploadIds()!=null){
                String[] strlist = item.getUploadIds().split(",");
                List<Long> listIds = new ArrayList<Long>(strlist.length);
                for (int i = 0; i < strlist.length; ++i)
                {
                    listIds.add( Long.parseLong(strlist[i]));
                }
                bizSafeAttach.setIds(listIds);
                /** 资料文档附件列表 */
                List<BizSafeAttach> fileDocAttachList = fileAttachService.selectBizSafeAttachListIds(bizSafeAttach);

                item2.setFileAttachList(fileDocAttachList);
            }else{
                item2.setFileAttachList(new ArrayList<>());
            }
            listReview.add(item2);
        }
        tQualityFileItemReviewDTO2.setListReview(listReview);

        /** 审批操作数据 */
        List<TQualityListCheckDTO> listCheck = new ArrayList<>();
        List<BizExtensionRequests> list2 = tQualityFileItemReviewDTO.getListTQualityCheck();
        for (BizExtensionRequests item : list2)
        {
            TQualityListCheckDTO item2 = new TQualityListCheckDTO();
            item2.setBucket(bucketName);
            /**
             * 记录id
             */
            item2.setRecordId(item.getRecordId());
            /**
             * 主键id
             */
            item2.setId(item.getId());
            /**
             * 申请人
             */
            item2.setUserName(item.getUserName());

            /**
             * 申请时间
             */
            item2.setDateTime(item.getDateTime());

            /**
             * 审批人
             */
            item2.setReplyUser(item.getReplyUser());

            /**
             * 审批时间
             */
            item2.setReplyTime(item.getReplyTime());

            /**
             * 状态-0不批准 1已批准
             */
            item2.setStatus(item.getStatus());

            /**
             * 申请延期至
             */
            item2.setExtensionTime(item.getExtensionTime());
            /*设置是否有权限*/
            item2.setUserType(item.getUserType());
//            if(tQualityFileItemReviewDTO.getExtensionId() != null && !tQualityFileItemReviewDTO.getExtensionId().isEmpty()){
//                if("3".equals(item.getUserType()) || "2".equals(item.getUserType())){
//                    item2.setUserType("2");
//                }else{
//                    item2.setUserType("4");
//                }
//            }else {
//                item2.setUserType(item.getUserType());
//            }

            /**
             * 原整改日期
             */
            item2.setFormerTime(item.getFormerTime());

            item2.setReason(item.getReason());
            item2.setReplyOpinion(item.getReplyOpinion());

            listCheck.add(item2);
        }
        tQualityFileItemReviewDTO2.setListCheck(listCheck);
        /** 移交操作数据 */
        List<TQualityListTransferDTO> listTransfer = new ArrayList<>();
        List<BizRectifyReview> list3 = tQualityFileItemReviewDTO.getListTransfer();
        for (BizRectifyReview item : list3)
        {
            TQualityListTransferDTO item2 = new TQualityListTransferDTO();
            item2.setBucket(bucketName);
            /**
             * 记录id
             */
            item2.setRecordId(item.getRecordId());
            /**
             * 序号
             */
            item2.setId(item.getId());

            /**
             * 操作人
             */
            item2.setOperatorer(item.getOperatorerName());

            /**
             * 操作时间
             */
            item2.setOperatorTime(item.getOperatorTime());

            /**
             * 移交人
             */
            item2.setRemoveUserName(item.getRemoveName());
            item2.setOperatorerPhonenumber(item.getOperatorerPhonenumber());
            item2.setRemoveUserPhonenumber(item.getRemoveUserPhonenumber());
            item2.setOperatorerName(item.getOperatorerName());
            listTransfer.add(item2);
        }
        tQualityFileItemReviewDTO2.setListTransfer(listTransfer);

        return tQualityFileItemReviewDTO2;
    }
    /**
     * 查询复查明细
     *
     * @param id 质量问题拆分主键
     * @return 质量问题复查明细
     */
    public TQualityReviewDTO selectReviewCheck(Long id)
    {
        /** 整改数据 */
//        TQualityFileItem tQualityFileItem = selectTQualityFileItemById(id);

        BizRectifyReview bizSelect = new BizRectifyReview();
        bizSelect.setRecordId(id);
        bizSelect.setOperatorType("1");
        /** 整改操作数据 */
        List<BizRectifyReview> listReview = iBizRectifyReviewService.selectBizRectifyReviewList(bizSelect);

        for (int i = listReview.size() - 1; i >= 0; --i)
        {
            BizRectifyReview item = listReview.get(i);
            TQualityReviewDTO tQualityReview = new TQualityReviewDTO();
            tQualityReview.setBizRectifyReview(item);
            tQualityReview.setBucket(bucketName);

            if (item.getOperatorType().equals("1"))
            {
                BizSafeFine bizSafeFine  = new BizSafeFine();
                bizSafeFine.setRecordId(id);
                bizSafeFine.setBizRectifyReviewId(item.getId());
                List<BizSafeFine> listBizSafeFine = bizSafeFineService.selectBizSafeFineList(bizSafeFine);
                tQualityReview.setListbizSafeFine(listBizSafeFine);
            }
            return tQualityReview;
        }
        return new TQualityReviewDTO();
    }

    /**
     * 查询复查明细-整改
     *
     * @param id 详情信息id, recordId 质量问题拆分主键
     * @return 质量问题整改-明细
     */
    public TQualityBizRectifyReviewDTO selectChildReview(Long id, Long recordId)
    {
        TQualityFileItemReviewDTO tQualityFileItemReview = new TQualityFileItemReviewDTO();
        /** 整改数据 */
        TQualityFileItem tQualityFileItem = selectTQualityFileItemById(recordId);

        if (tQualityFileItem == null)
        {
            tQualityFileItemReview.settQualityFileItem(null);
        }
        else
        {
            // 超期计算
            if (tQualityFileItem.getReviseTheDates() == null)
            {
                tQualityFileItem.setOverdue("0");
            }
            else
            {
                Date last = tQualityFileItem.getReviseTheDates();
                last.setHours(23);
                last.setMinutes(59);
                last.setSeconds(59);
                int v = last.compareTo(DateUtils.getNowDate());
                tQualityFileItem.setOverdue(v >= 0 ? "0" : "1");
            }

            //如果当前问题没有查到检查人，则此问题若有发文单位，则发文单位即为检查人名称
            if (tQualityFileItem.getCheckPeopleName() == null || tQualityFileItem.getCheckPeopleName().isEmpty())
            {
                if (tQualityFileItem.getFileCompanyName() != null && !tQualityFileItem.getFileCompanyName().isEmpty())
                {
                    tQualityFileItem.setCheckPeopleName(tQualityFileItem.getFileCompanyName());
                }
            }
            tQualityFileItem.setBucket(bucketName);
            tQualityFileItemReview.settQualityFileItem(tQualityFileItem);
        }

        BizRectifyReview bizSelect = new BizRectifyReview();
        bizSelect.setRecordId(recordId);
        bizSelect.setOperatorType("1");
        /** 整改操作数据 */
        List<BizRectifyReview> listReview = iBizRectifyReviewService.selectBizRectifyReviewList(bizSelect);

        List<TQualityReviewDTO> listTQualityReviews = new ArrayList<>();

        for (int i = listReview.size() - 1; i >= 0; --i)
        {
            BizRectifyReview item = listReview.get(i);
            TQualityReviewDTO tQualityReview = new TQualityReviewDTO();
            tQualityReview.setBizRectifyReview(item);
            BizSafeFine bizSafeFine  = new BizSafeFine();
            bizSafeFine.setRecordId(recordId);
            bizSafeFine.setBizRectifyReviewId(item.getId());
            List<BizSafeFine> listBizSafeFine = bizSafeFineService.selectBizSafeFineList(bizSafeFine);
            if (listBizSafeFine == null)
            {
                tQualityReview.setListbizSafeFine(new ArrayList<>());
            }
            else
            {
                tQualityReview.setListbizSafeFine(listBizSafeFine);
            }
            tQualityReview.setOperatorExplain(item.getOperatorExplain());
            tQualityReview.setQuestionOrigin(tQualityFileItemReview.gettQualityFileItem().getQuestionOrigin());
            listTQualityReviews.add(tQualityReview);
        }
        if (listTQualityReviews == null)
        {
            tQualityFileItemReview.setListTQualityReview(new ArrayList<>());
        }
        else
        {
            tQualityFileItemReview.setListTQualityReview(listTQualityReviews);
        }

        //================================
        TQualityBizRectifyReviewDTO tQualityBizRectifyReviewDTO = new TQualityBizRectifyReviewDTO();
        tQualityBizRectifyReviewDTO.setBucket(bucketName);
        if (tQualityFileItemReview == null)
        {
            tQualityBizRectifyReviewDTO.setBucket(bucketName);
            tQualityBizRectifyReviewDTO.setQuestionOrigin("");
            tQualityBizRectifyReviewDTO.setListBizRectifyReview(new ArrayList<>());
            return tQualityBizRectifyReviewDTO;
        }
        tQualityBizRectifyReviewDTO.setBucket(bucketName);
        if (tQualityFileItemReview.gettQualityFileItem() == null)
        {
            //成因在主表中
            tQualityBizRectifyReviewDTO.setQuestionOrigin("");
        }
        else
        {
            //成因在主表中
            tQualityBizRectifyReviewDTO.setQuestionOrigin(tQualityFileItemReview.gettQualityFileItem().getQuestionOrigin());
        }
        List<TQualityReviewDTO> list = tQualityFileItemReview.getListTQualityReview();
        List<BizRectifyReviewDTO> listBizRectifyReview= new ArrayList<>();
        TQualityReviewDTO itemTemp = null;

        for (int i = 0; i < list.size(); ++i)
        {
            TQualityReviewDTO item = list.get(i);
            item.setBucket(bucketName);
            if (id != null)
            {
                if (!item.getBizRectifyReview().getId().equals(id))
                {
                    continue;
                }
            }
            if (item.getListbizSafeFine() != null)
            {
                List<BizSafeFine> list2 = item.getListbizSafeFine();
                for (BizSafeFine item2 : list2)
                {
                    BizRectifyReviewDTO bizRectifyReviewDTO = new BizRectifyReviewDTO();
                    bizRectifyReviewDTO.setBucket(bucketName);

                    BeanUtil.copyProperties(item.getBizRectifyReview(), bizRectifyReviewDTO);
                    bizRectifyReviewDTO.setFineExplain(item2.getFineExplain());
                    bizRectifyReviewDTO.setOperatorPhoto(item2.getFineAttachment());
                    bizRectifyReviewDTO.setOperatorExplain(item2.getOperatorExplain());
                    bizRectifyReviewDTO.setFineMoney(item2.getFineMoney());
                    if (item2.getFineMoney() == null || item2.getFineMoney().equals(0.0))
                    {
                        bizRectifyReviewDTO.setIsFine("0");
                    }
                    else
                    {
                        bizRectifyReviewDTO.setIsFine("1");
                    }
                    if (item2.getFineButton().equals("1"))
                    {
                        bizRectifyReviewDTO.setSubpackageUnitIds(item2.getFineUser());
                    }
                    else if (item2.getFineButton().equals("2"))
                    {
                        bizRectifyReviewDTO.setSubpackageUnitIds("");
                    }
                    bizRectifyReviewDTO.setProjectId(item2.getProjectId());
                    bizRectifyReviewDTO.setRecordId(item2.getRecordId());
                    bizRectifyReviewDTO.setId(item2.getBizRectifyReviewId());

                    if(StringUtils.isNotNull(bizRectifyReviewDTO.getOperatorPhoto()) && !"".equals(bizRectifyReviewDTO.getOperatorPhoto())){
                        String[] arr = bizRectifyReviewDTO.getOperatorPhoto().split(",");
                        Long[] photoIds = new Long[arr.length];
                        for(int j=0;j<arr.length;j++){
                            photoIds[j] = Long.parseLong(arr[j]);
                        }
                        List<BizSafeAttach> bizSafeAttaches = bizSafeAttachService.selectBizSafeAttachByBizIds(photoIds);
                        bizRectifyReviewDTO.setOperatorPhotos(bizSafeAttaches);
                    }

                    listBizRectifyReview.add(bizRectifyReviewDTO);
                }
            }
            break;
        }
        tQualityBizRectifyReviewDTO.setListBizRectifyReview(listBizRectifyReview);

        return tQualityBizRectifyReviewDTO;
    }

    /**
     * 查询复查明细-复查
     *
     * @param id 详情信息id, recordId 质量问题拆分主键
     * @return 质量问题-复查明细
     */
    public TQualityBizRectifyReviewDTO2 selectChildReview2(Long id, Long recordId)
    {
        TQualityBizRectifyReviewDTO2 tQualityBizRectifyReviewDTO2 = new TQualityBizRectifyReviewDTO2();
        tQualityBizRectifyReviewDTO2.setBucket(bucketName);
        tQualityBizRectifyReviewDTO2.setBizRectifyReview(null);
        tQualityBizRectifyReviewDTO2.setQuestionOrigin("");
        tQualityBizRectifyReviewDTO2.setListBizRectifyReview(new ArrayList<>());

        TQualityFileItemReviewDTO tQualityFileItemReview = new TQualityFileItemReviewDTO();
        tQualityFileItemReview.setBucket(bucketName);
        /** 整改数据 */
        TQualityFileItem tQualityFileItem = selectTQualityFileItemById(recordId);

        if (tQualityFileItem == null)
        {
            tQualityFileItemReview.settQualityFileItem(null);
        }
        else
        {
            // 超期计算
            if (tQualityFileItem.getReviseTheDates() == null)
            {
                tQualityFileItem.setOverdue("0");
            }
            else
            {
                Date last = tQualityFileItem.getReviseTheDates();
                last.setHours(23);
                last.setMinutes(59);
                last.setSeconds(59);
                int v = last.compareTo(DateUtils.getNowDate());
                tQualityFileItem.setOverdue(v >= 0 ? "0" : "1");
            }

            //如果当前问题没有查到检查人，则此问题若有发文单位，则发文单位即为检查人名称
            if (tQualityFileItem.getCheckPeopleName() == null || tQualityFileItem.getCheckPeopleName().isEmpty())
            {
                if (tQualityFileItem.getFileCompanyName() != null && !tQualityFileItem.getFileCompanyName().isEmpty())
                {
                    tQualityFileItem.setCheckPeopleName(tQualityFileItem.getFileCompanyName());
                }
            }
            tQualityFileItem.setBucket(bucketName);
            tQualityFileItemReview.settQualityFileItem(tQualityFileItem);
        }

        BizRectifyReview bizSelect = new BizRectifyReview();
        bizSelect.setRecordId(recordId);
        /** 整改操作数据 */
        List<BizRectifyReview> listReview = iBizRectifyReviewService.selectBizRectifyReviewList2(bizSelect);

        List<TQualityReviewDTO> listTQualityReviews = new ArrayList<>();
        for (BizRectifyReview item : listReview)
        {
            TQualityReviewDTO tQualityReview = new TQualityReviewDTO();
            tQualityReview.setBizRectifyReview(item);
            tQualityReview.setBucket(bucketName);

            if (item.getOperatorType().equals("1"))
            {
                BizSafeFine bizSafeFine  = new BizSafeFine();
                bizSafeFine.setRecordId(recordId);
                bizSafeFine.setBizRectifyReviewId(item.getId());
                List<BizSafeFine> listBizSafeFine = bizSafeFineService.selectBizSafeFineList(bizSafeFine);
                if (listBizSafeFine == null)
                {
                    tQualityReview.setListbizSafeFine(new ArrayList<>());
                }
                else
                {
                    tQualityReview.setListbizSafeFine(listBizSafeFine);
                }
            }
            listTQualityReviews.add(tQualityReview);
        }
        if (listTQualityReviews == null)
        {
            tQualityFileItemReview.setListTQualityReview(new ArrayList<>());
        }
        else
        {
            tQualityFileItemReview.setListTQualityReview(listTQualityReviews);
        }

        //================================

        if (tQualityFileItemReview == null)
        {
            return tQualityBizRectifyReviewDTO2;
        }
        if (tQualityFileItemReview.gettQualityFileItem() == null)
        {
            //成因在主表中
            tQualityBizRectifyReviewDTO2.setQuestionOrigin("");
        }
        else
        {
            //成因在主表中
            tQualityBizRectifyReviewDTO2.setQuestionOrigin(tQualityFileItemReview.gettQualityFileItem().getQuestionOrigin());
        }
        List<TQualityReviewDTO> list = tQualityFileItemReview.getListTQualityReview();
        int nIndex = -1;
        int nIndex2 = -1;
        BizRectifyReview bizRectifyReview = null;

        for (int i = list.size() - 1; i >= 0; --i)
        {
            TQualityReviewDTO item = list.get(i);
            item.setBucket(bucketName);
            if (!item.getBizRectifyReview().getId().equals(id))
            {
                continue;
            }
            bizRectifyReview = item.getBizRectifyReview();
            nIndex = i;
            break;
        }
        if (nIndex == -1)
        {
            nIndex = list.size() - 1;
        }

        if (bizRectifyReview == null)
        {
            bizRectifyReview = new BizRectifyReview();
        }

        tQualityBizRectifyReviewDTO2.setBizRectifyReview(bizRectifyReview);
        List<BizRectifyReviewDTO> listBizRectifyReview= new ArrayList<>();
        tQualityBizRectifyReviewDTO2.setBucket(bucketName);

        for (int i = nIndex; i >= 0; --i)
        {
            TQualityReviewDTO item = list.get(i);
            if (!item.getBizRectifyReview().getOperatorType().equals("1"))
            {
                continue;
            }
            item.setBucket(bucketName);

            List<BizSafeFine> list2 = item.getListbizSafeFine();
            for (BizSafeFine item2 : list2)
            {
                BizRectifyReviewDTO bizRectifyReviewDTO = new BizRectifyReviewDTO();
                BeanUtil.copyProperties(item.getBizRectifyReview(), bizRectifyReviewDTO);
                bizRectifyReviewDTO.setBucket(bucketName);
                bizRectifyReviewDTO.setFineExplain(item2.getFineExplain());
                bizRectifyReviewDTO.setOperatorPhoto(item2.getFineAttachment());
                bizRectifyReviewDTO.setOperatorExplain(item2.getOperatorExplain());
                bizRectifyReviewDTO.setFineMoney(item2.getFineMoney());
                if (item2.getFineMoney() == null || item2.getFineMoney().equals(0.0))
                {
                    bizRectifyReviewDTO.setIsFine("0");
                }
                else
                {
                    bizRectifyReviewDTO.setIsFine("1");
                }
                if (item2.getFineButton().equals("1"))
                {
                    bizRectifyReviewDTO.setSubpackageUnitIds(item2.getFineUser());
                }
                else if (item2.getFineButton().equals("2"))
                {
                    bizRectifyReviewDTO.setSubpackageUnitIds("");
                }
                bizRectifyReviewDTO.setProjectId(item2.getProjectId());
                bizRectifyReviewDTO.setRecordId(item2.getRecordId());
                bizRectifyReviewDTO.setId(item2.getBizRectifyReviewId());
                listBizRectifyReview.add(bizRectifyReviewDTO);
            }
            tQualityBizRectifyReviewDTO2.setListBizRectifyReview(listBizRectifyReview);
            break;
        }

        return tQualityBizRectifyReviewDTO2;
    }

    /**
     * 查询复查明细-延期申请
     *
     * @param id 详情信息id, recordId 质量问题拆分主键
     * @return 质量问题延期申请明细
     */
    public BizExtensionRequests selectChildCheck(Long id, Long recordId)
    {
        TQualityFileItemReviewDTO tQualityFileItemReview = new TQualityFileItemReviewDTO();
        /** 整改数据 */
        TQualityFileItem tQualityFileItem = selectTQualityFileItemById(recordId);

        if (tQualityFileItem == null)
        {
            tQualityFileItemReview.settQualityFileItem(null);
        }
        else
        {
            // 超期计算
            if (tQualityFileItem.getReviseTheDates() == null)
            {
                tQualityFileItem.setOverdue("0");
            }
            else
            {
                Date last = tQualityFileItem.getReviseTheDates();
                last.setHours(23);
                last.setMinutes(59);
                last.setSeconds(59);
                int v = last.compareTo(DateUtils.getNowDate());
                tQualityFileItem.setOverdue(v >= 0 ? "0" : "1");
            }

            //如果当前问题没有查到检查人，则此问题若有发文单位，则发文单位即为检查人名称
            if (tQualityFileItem.getCheckPeopleName() == null || tQualityFileItem.getCheckPeopleName().isEmpty())
            {
                if (tQualityFileItem.getFileCompanyName() != null && !tQualityFileItem.getFileCompanyName().isEmpty())
                {
                    tQualityFileItem.setCheckPeopleName(tQualityFileItem.getFileCompanyName());
                }
            }
            tQualityFileItem.setBucket(bucketName);
            tQualityFileItemReview.settQualityFileItem(tQualityFileItem);
        }

        BizRectifyReview bizSelect = new BizRectifyReview();
        bizSelect.setRecordId(recordId);
        /** 审批操作数据 */
        BizExtensionRequests bizExtensionRequests = new BizExtensionRequests();
        bizExtensionRequests.setRecordId(recordId);
        List<BizExtensionRequests> listCheck = bizExtensionRequestsService.selectBizExtensionRequestsList(bizExtensionRequests);
        if (listCheck == null)
        {
            tQualityFileItemReview.setListTQualityCheck(new ArrayList<>());
        }
        else
        {
            tQualityFileItemReview.setListTQualityCheck(listCheck);
        }

        if (tQualityFileItemReview == null)
        {
            return null;
        }
        List<BizExtensionRequests> list = tQualityFileItemReview.getListTQualityCheck();
        for (int i = list.size() - 1; i >= 0; --i)
        {
            BizExtensionRequests item = list.get(i);
            item.setBucket(bucketName);
            if (id == null)
            {
                return item;
            }
            if (!item.getId().equals(id))
            {
                continue;
            }
            return  item;
        }
        return null;
    }

    /**
     * 询复查明细-移交
     *
     * @param id 详情信息id, recordId 质量问题拆分主键
     * @return 质量问题移交明细
     */
    public BizRectifyReview selectChildTransfer(Long id, Long recordId)
    {
        TQualityFileItemReviewDTO tQualityFileItemReview = new TQualityFileItemReviewDTO();
        tQualityFileItemReview.setBucket(bucketName);
        /** 整改数据 */
        TQualityFileItem tQualityFileItem = selectTQualityFileItemById(recordId);

        if (tQualityFileItem == null)
        {
            tQualityFileItemReview.settQualityFileItem(null);
        }
        else
        {
            // 超期计算
            if (tQualityFileItem.getReviseTheDates() == null)
            {
                tQualityFileItem.setOverdue("0");
            }
            else
            {
                Date last = tQualityFileItem.getReviseTheDates();
                last.setHours(23);
                last.setMinutes(59);
                last.setSeconds(59);
                int v = last.compareTo(DateUtils.getNowDate());
                tQualityFileItem.setOverdue(v >= 0 ? "0" : "1");
            }

            //如果当前问题没有查到检查人，则此问题若有发文单位，则发文单位即为检查人名称
            if (tQualityFileItem.getCheckPeopleName() == null || tQualityFileItem.getCheckPeopleName().isEmpty())
            {
                if (tQualityFileItem.getFileCompanyName() != null && !tQualityFileItem.getFileCompanyName().isEmpty())
                {
                    tQualityFileItem.setCheckPeopleName(tQualityFileItem.getFileCompanyName());
                }
            }
            tQualityFileItemReview.settQualityFileItem(tQualityFileItem);
        }

        BizRectifyReview bizSelect = new BizRectifyReview();
        bizSelect.setRecordId(recordId);

        /** 移交操作数据 */
        bizSelect.setOperatorType("3");
        List<BizRectifyReview> listTransfer = iBizRectifyReviewService.selectBizRectifyReviewList(bizSelect);
        if (listTransfer == null)
        {
            tQualityFileItemReview.setListTransfer(new ArrayList<>());
        }
        else
        {
            for (BizRectifyReview item : listTransfer)
            {
                item.setRemoveUserName(item.getRemoveName());
            }
            tQualityFileItemReview.setListTransfer(listTransfer);
        }

        if (tQualityFileItemReview == null)
        {
            return null;
        }
        List<BizRectifyReview> list = tQualityFileItemReview.getListTransfer();
        for (int i = list.size() - 1; i >= 0; --i)
        {
            BizRectifyReview item = list.get(i);
            item.setBucket(bucketName);
            if (id == null)
            {
                return item;
            }
            if (!item.getId().equals(id))
            {
                continue;
            }

            return  item;
        }
        return null;
    }


    /**
     * 查询质量问题我的审核列表
     *
     * @param tQualityFileItem 质量问题我的审核列表
     * @return 质量问题我的审核列表
     */
    @Override
    public List<TQualityFileItem> selectExtensionLedger(TQualityFileItem tQualityFileItem)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        tQualityFileItem.setLoginUserId(loginUser.getUserId().toString());
        List<TQualityFileItem> list = tQualityFileItemMapper.selectExtensionLedger(tQualityFileItem);
        for (TQualityFileItem item : list)
        {
            if(item.getFileIds()!=null && !"".equals(item.getFileIds())){
                BizSafeAttach bizSafeAttach = new BizSafeAttach();
                String[] strlist = item.getFileIds().split(",");
                List<Long> listIds = new ArrayList<Long>(strlist.length);
                for (int i = 0; i < strlist.length; ++i)
                {
                    listIds.add( Long.parseLong(strlist[i]));
                }
                bizSafeAttach.setIds(listIds);
                /** 资料文档附件列表 */
                List<BizSafeAttach> fileDocAttachList = fileAttachService.selectBizSafeAttachListIds(bizSafeAttach);
                for(int i=0;i< fileDocAttachList.size();i++){
                    BizSafeAttach attach = fileDocAttachList.get(i);
                    if(!attach.getType().contains("image")){
                        fileDocAttachList.remove(attach);
                    }
                }
                item.setAttachList(fileDocAttachList);
            }else{
                item.setAttachList(new ArrayList<>());
            }
            // 超期计算
            if (item.getReviseTheDates() == null)
            {
                item.setOverdue("0");
            }
            else
            {
                Date last = item.getReviseTheDates();
                last.setHours(23);
                last.setMinutes(59);
                last.setSeconds(59);
                int v = last.compareTo(DateUtils.getNowDate());
                item.setOverdue(v >= 0 ? "0" : "1");
            }

            //如果当前问题没有查到检查人，则此问题若有发文单位，则发文单位即为检查人名称
            if (item.getCheckPeopleName() == null || item.getCheckPeopleName().isEmpty())
            {
                if (item.getFileCompanyName() != null && !item.getFileCompanyName().isEmpty())
                {
                    item.setCheckPeopleName(item.getFileCompanyName());
                }
            }
            /*如果查询有待审核数据*/
                if("0".equals(item.getStatus())){
                    if("2".equals(item.getDataStatus()) | "3".equals(item.getDataStatus())){
                        item.setUserType("2");
                    }else{
                        item.setUserType("4");
                    }
                }else{
                    item.setUserType("4");
                }
//                if("3".equals(item.getUserType())){
//                    if("0".equals(item.getDataStatus())){
//                        item.setUserType("1");
//                    }else if("1".equals(item.getDataStatus())){
//                        item.setUserType("2");
//                    }else{
//                        item.setUserType("4");
//                    }
//                }
            item.setBucket(bucketName);
        }
        return list;
    }


    /**
     * 新增质量问题拆分明细
     * 
     * @param tQualityFileItem 质量问题拆分明细
     * @return 结果
     */
    @Override
    public int insertTQualityFileItem(TQualityFileItem tQualityFileItem)
    {
        tQualityFileItem.setDataStatus("0");
        tQualityFileItem.setCreateTime(DateUtils.getNowDate());
        LoginUser loginUser = SecurityUtils.getLoginUser();
        tQualityFileItem.setCreateBy(loginUser.getUserId().toString());
        if (tQualityFileItem.getFileId() == null)
        {
            tQualityFileItem.setCheckPeopleId(loginUser.getUserId().toString());
        }
        return tQualityFileItemMapper.insertTQualityFileItem(tQualityFileItem);
    }

    /**
     * 修改质量问题拆分明细
     * 
     * @param tQualityFileItem 质量问题拆分明细
     * @return 结果
     */
    @Override
    public int updateTQualityFileItem(TQualityFileItem tQualityFileItem)
    {
        tQualityFileItem.setUpdateTime(DateUtils.getNowDate());
        LoginUser loginUser = SecurityUtils.getLoginUser();
        tQualityFileItem.setUpdateBy(loginUser.getUserId().toString());
        return tQualityFileItemMapper.updateTQualityFileItem(tQualityFileItem);
    }

    /**
     * 批量删除质量问题拆分明细
     * 
     * @param ids 需要删除的质量问题拆分明细主键
     * @return 结果
     */
    @Override
    public int deleteTQualityFileItemByIds(Long[] ids)
    {
        return tQualityFileItemMapper.deleteTQualityFileItemByIds(ids);
    }

    /**
     * 删除质量问题拆分明细信息
     * 
     * @param id 质量问题拆分明细主键
     * @return 结果
     */
    @Override
    public int deleteTQualityFileItemById(Long id)
    {
        return tQualityFileItemMapper.deleteTQualityFileItemById(id);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean addRectifyReview(BizRectifyReviewDTO bizRectifyReviewDTO) {
        // 复查整改数据从dto复制到实体类
        BizRectifyReview rectifyReview = new BizRectifyReview();
        BeanUtil.copyProperties(bizRectifyReviewDTO, rectifyReview);
        if (StrUtil.isNotEmpty(SecurityUtils.getUsername())) {
            bizRectifyReviewDTO.setUserName(SecurityUtils.getUsername());
        }
        // 主键id，使用雪花算法获取
        rectifyReview.setId(IdUtil.getSnowflakeNextId());

        // 查询项目自检-检查记录
        TQualityFileItem tQualityFileItem = selectTQualityFileItemById(bizRectifyReviewDTO.getRecordId());
        rectifyReview.setOperatorType("2");
        if (rectifyReview.getOperatorStatus().equals("2"))
        {
            tQualityFileItem.setReviewTime(new Date());
            tQualityFileItem.setDataStatus("0");
        }
        else if (rectifyReview.getOperatorStatus().equals("3"))
        {
            tQualityFileItem.setReviewTime(new Date());
            tQualityFileItem.setDataStatus("2");
        }

        tQualityFileItem.setUpdateBy(bizRectifyReviewDTO.getUserName());
        tQualityFileItem.setUpdateTime(new Date());
        tQualityFileItem.setCanDel("2");

        updateTQualityFileItem(tQualityFileItem);

        LoginUser loginUser = SecurityUtils.getLoginUser();
        // 保存复查整改数据
        rectifyReview.setOperatorer(loginUser.getUsername());
        rectifyReview.setOperatorTime(DateUtils.getNowDate());
        rectifyReview.setCreateBy(loginUser.getUsername());
        rectifyReview.setCreateTime(DateUtils.getNowDate());
        iBizRectifyReviewService.insertBizRectifyReview(rectifyReview);

        return true;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean addRectifyReview(TQualityBizRectifyReviewDTO tQualityBizRectifyReview) {
        List<BizRectifyReviewDTO> listBizRectifyReviews = tQualityBizRectifyReview.getListBizRectifyReview();
        if (listBizRectifyReviews.size() > 0)
        {
            // 复查整改数据从dto复制到实体类
            BizRectifyReview rectifyReview = new BizRectifyReview();
            BeanUtil.copyProperties(listBizRectifyReviews.get(0), rectifyReview);
            // 主键id，使用雪花算法获取
            rectifyReview.setId(IdUtil.getSnowflakeNextId());


            // 查询项目自检-检查记录
            TQualityFileItem tQualityFileItem = selectTQualityFileItemById(
                    listBizRectifyReviews.get(0).getRecordId());

            //去掉重复和空字符串的分包单位
            Set<String> subpackageUnitAll = new HashSet<>();

            if (ObjectUtil.isNotEmpty(tQualityFileItem.getSubpackageUnit())) {
                subpackageUnitAll.addAll(Arrays.stream(tQualityFileItem.getSubpackageUnit().split(","))
                        .filter(StrUtil::isNotEmpty).collect(Collectors.toSet()));
            }

            for (BizRectifyReviewDTO bizRectifyReviewDTO : listBizRectifyReviews)
            {
                if (StrUtil.isNotEmpty(SecurityUtils.getUsername())) {
                    bizRectifyReviewDTO.setUserName(SecurityUtils.getUsername());
                }

                String subpackageUnit = bizRectifyReviewDTO.getSubpackageUnitIds();
                SysUser sysUser = sysUserMapper.selectUserByUserName(bizRectifyReviewDTO.getUserName());
                SysDept sysDept = sysDeptMapper.selectDeptById(sysUser.getDeptId());
                CommonProject project = commonProjectMapper.selectCommonProjectById(
                        bizRectifyReviewDTO.getProjectId());
                //查询登陆人所在部门
                if (subpackageUnit == null || subpackageUnit.isEmpty()) {

                    bizSafeFineService.insertBizSafeFine(BizSafeFine.builder()
                            .safeNumber(bizSafeBillnoService.getSafeNumber(1L))
                            .fineUnit(sysDept.getDeptName())
                            .fineUser(project.getName())
                            .fineExplain(bizRectifyReviewDTO.getFineExplain())
                            .fineAttachment(bizRectifyReviewDTO.getOperatorPhoto())
                            .operatorExplain(bizRectifyReviewDTO.getOperatorExplain())
                            .fineSource("1")
                            .fineMoney(bizRectifyReviewDTO.getFineMoney())
                            .projectId(bizRectifyReviewDTO.getProjectId())
                            .recordId(bizRectifyReviewDTO.getRecordId())
                            .fineButton("2")
                            .createBy(bizRectifyReviewDTO.getUserName())
                            .createTime(new Date())
                            .bizRectifyReviewId(rectifyReview.getId())
                            .build(),sysUser);

                }
                else
                {
                    bizSafeFineService.insertBizSafeFine(BizSafeFine.builder()
                            .safeNumber(bizSafeBillnoService.getSafeNumber(1L))
                            .fineUnit(sysDept.getDeptName())
                            .fineUser(subpackageUnit)
                            .fineExplain(bizRectifyReviewDTO.getFineExplain())
                            .fineAttachment(bizRectifyReviewDTO.getOperatorPhoto())
                            .operatorExplain(bizRectifyReviewDTO.getOperatorExplain())
                            .fineSource("1")
                            .fineMoney(bizRectifyReviewDTO.getFineMoney())
                            .projectId(bizRectifyReviewDTO.getProjectId())
                            .recordId(bizRectifyReviewDTO.getRecordId())
                            .fineButton("1")
                            .createBy(bizRectifyReviewDTO.getUserName())
                            .createTime(new Date())
                            .bizRectifyReviewId(rectifyReview.getId())
                            .build(),sysUser);
                }

                // 保存附件
                //bizSafeAttachService.selectBizSafeAttachByNames(bizRectifyReviewDTO.getFileName(), rectifyReview.getId());

                if (ObjectUtil.isNotEmpty(listBizRectifyReviews.get(0).getSubpackageUnitIds())) {
                    subpackageUnitAll.addAll(Arrays.stream(listBizRectifyReviews.get(0).getSubpackageUnitIds().split(","))
                            .filter(StrUtil::isNotEmpty).collect(Collectors.toSet()));
                }
            }

            tQualityFileItem.setSubpackageUnit(StrUtil.join(",", subpackageUnitAll));

            rectifyReview.setOperatorType("1");
            if (rectifyReview.getOperatorStatus().equals("1"))
            {
                tQualityFileItem.setCheckTime(new Date());
                tQualityFileItem.setDataStatus("1");
            }

            LoginUser loginUser = SecurityUtils.getLoginUser();
            tQualityFileItem.setUpdateBy(loginUser.getUsername());
            tQualityFileItem.setUpdateTime(new Date());
            tQualityFileItem.setCanDel("2");
            tQualityFileItem.setQuestionOrigin(tQualityBizRectifyReview.getQuestionOrigin());

            updateTQualityFileItem(tQualityFileItem);

            // 保存复查整改数据
            rectifyReview.setOperatorer(loginUser.getUsername());
            rectifyReview.setOperatorTime(DateUtils.getNowDate());
            rectifyReview.setCreateBy(loginUser.getUsername());
            rectifyReview.setCreateTime(DateUtils.getNowDate());
            iBizRectifyReviewService.insertBizRectifyReview(rectifyReview);

            // 保存附件
            //bizSafeAttachService.selectBizSafeAttachByNames(bizRectifyReviewDTO.getFileName(), rectifyReview.getId());
        }
        return true;
    }

    @Override
    public TIndexData2 getQuestionInfo(String projectId) {
        return tQualityFileItemMapper.getQuestionInfo(Long.parseLong(projectId));
    }

}
